package com.zhengjiang.sms.consumer.controller;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.zhengjiang.sms.common.ResponseDto;
import com.zhengjiang.sms.consumer.model.SmsQueryDto;
import com.zhengjiang.sms.consumer.service.ISendSmsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName SmsLoginController
 * @Description
 * @Author zhengjiang(99514925 @ qq.com)
 * @Date 2020-06-02 16:39
 * @Version V1.0
 **/
@RestController
public class SmsLoginController {

    @Autowired
    ISendSmsService sendSmsService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 功能描述: 手机验证码登录
     * @Param :
     * @Return:
     * @Date: 2020-06-02 17:26
    */
    @PostMapping("login")
    public ResponseDto login(@RequestParam("phone") String phone,@RequestParam("code") String code){
        if(StringUtils.isEmpty(phone)){
            return ResponseDto.fail("手机号码不能为空");
        }
        if (StringUtils.isEmpty(code)){
            return ResponseDto.fail("验证码不能为空");
        }
        String rcode = (String) redisTemplate.opsForValue().get(phone);
        if(StringUtils.isEmpty(rcode)){
            return ResponseDto.fail("验证码已过期");
        }else if(StringUtils.equals(code,rcode)){
            return ResponseDto.success("登录成功");
        }
        return ResponseDto.fail();
    }
    /**
     * 功能描述:短信发送记录查询
     * @Param :
     * @Return:
     * @Date: 2020-05-29 15:09
     */
    @PostMapping("sendDetail")
    public QuerySendDetailsResponse query(@RequestBody SmsQueryDto smsQueryDto){
        QuerySendDetailsResponse querySendDetailsResponse = sendSmsService.querySendDetails(smsQueryDto);
        return querySendDetailsResponse;
    }




}
